package com.sa.oauth.handler;

import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.data.model.ClientTokenModel;
import cn.dev33.satoken.oauth2.scope.handler.SaOAuth2ScopeHandlerInterface;
import com.alibaba.fastjson.JSON;
import com.sa.oauth.entity.OAuthClient;
import com.sa.oauth.entity.SysPerson;
import com.sa.oauth.mapper.OAuthClientMapper;
import com.sa.oauth.service.Oauth2UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Arrays;

@Slf4j
@Component
public class UserinfoScopeHandler implements SaOAuth2ScopeHandlerInterface {

    @Resource
    private Oauth2UserService oauth2userService;
    @Autowired
    private OAuthClientMapper oAuthClientMapper;

    @Override
    public String getHandlerScope() {
        return "userinfo";
    }

    @Override
    public void workAccessToken(AccessTokenModel accessTokenModel) {
        log.info("userinfo scope work access token: {}", accessTokenModel);

        Object loginId = accessTokenModel.getLoginId();
        // 从数据库查询用户信息
        SysPerson sysPerson = oauth2userService.getById(Long.valueOf(loginId.toString()));
        accessTokenModel.extraData.put("userinfo", JSON.toJSONString(sysPerson));
    }

    @Override
    public void workClientToken(ClientTokenModel clientTokenModel) {

    }
}
